package mobweb.booktrack;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

public class AddBookActivity extends Activity {

	EditText bookTitle,bookAuthor,bookIsbn,bookDescription;
	int flag,idx;
	String bookTitleTxt,bookAuthorTxt,bookDescriptionTxt;
	String bookIsbnTxt; 
	private final Activity act = this;
	
		@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_book);
		
		
		Button saveButton = (Button) findViewById(R.id.savebookbutton);
		saveButton.setOnClickListener(new OnClickListener() {
			
			@SuppressLint({ "NewApi", "ShowToast" })
			@Override
			public void onClick(View v) {
				/**
				 * getting data from EditText and the chosen radiobutton
				 */
				bookTitle = (EditText) findViewById(R.id.editText1);
				bookAuthor = (EditText) findViewById(R.id.editText2);
				bookIsbn = (EditText) findViewById(R.id.editText3);
				bookDescription = (EditText) findViewById(R.id.editText4);
				
				bookTitleTxt = bookTitle.getText().toString().trim();
				bookAuthorTxt = bookAuthor.getText().toString().trim();
				bookIsbnTxt = bookIsbn.getText().toString();
				bookDescriptionTxt = bookDescription.getText().toString().trim();
				RadioGroup choice = (RadioGroup) findViewById(R.id.addRadioGroup);
				
				flag = choice.getCheckedRadioButtonId();
				View radioButton = choice.findViewById(flag);
				idx = choice.indexOfChild(radioButton);

				// TODO Auto-generated method stub
				/**
				 * introducing a book in a database, only if: --the author and title fileds are not empty --the query was succesful! 
				 */
				Book addBook = new Book( bookTitleTxt, bookDescriptionTxt,bookAuthorTxt, "comedy", bookIsbnTxt, idx, -1);
				
				if(!(bookTitleTxt).equals("") && !(bookAuthorTxt).equals("")){
					if(addBook.writeToDB(getBaseContext())) {
						Toast.makeText(getBaseContext(), "The book with title: " + addBook.getBookName().toString().trim() + " was added!",Toast.LENGTH_LONG).show();
						NavUtils.navigateUpFromSameTask(act);
					}
					else
						Toast.makeText(getBaseContext(), "An error occured.\n the book with title:" + addBook.getBookName().toString().trim() +
							"was NOT added!",Toast.LENGTH_LONG).show();
					
					}
				else
					Toast.makeText(getBaseContext(), "Please provide us at least: The author and the title of the book!", Toast.LENGTH_LONG).show();
			}
		});
		
		
		Button button2 = (Button) findViewById(R.id.canceladdbookbutton);
		button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent home = new Intent(getApplicationContext(),MainMenu.class);
				startActivity(home);
				
			}
		});
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_add_bokk, menu);
		return true;
	}

}
